# Maintainer: Ilya Rakhlin <i.rakhlin@gmail.com>

_realname=pkgconf
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
epoch=1
pkgver=2.3.0
pkgrel=1
pkgdesc='pkg-config compatible utility which does not depend on glib'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/pkgconf/pkgconf'
msys2_references=(
  "archlinux: pkgconf"
  "cpe: cpe:/a:pkgconf:pkgconf"
)
license=('spdx:ISC')
provides=("${MINGW_PACKAGE_PREFIX}-pkg-config")
conflicts=("${MINGW_PACKAGE_PREFIX}-pkg-config")
replaces=("${MINGW_PACKAGE_PREFIX}-pkg-config")
makedepends=("${MINGW_PACKAGE_PREFIX}-meson" "${MINGW_PACKAGE_PREFIX}-cc")
groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
source=("https://github.com/pkgconf/pkgconf/archive/pkgconf-$pkgver/$_realname-$pkgver.tar.gz"
        0002-size-t-format.patch
        0003-printf-format.patch
        0004-default-pure-static.patch)
sha256sums=('0ee103cd390c3ee0e77a7a1c71dcb79a50a426fa2a648f6d07f2678c23adc5e3'
            '1db5975d6b1db946ba1d71cc3cf390dec567899a89d7ef53f56f24f0058cdbe3'
            'a7efeef61b4af1c0813d9d8a211cde669a3e3fa2c97ed21e55487667769c567a'
            '93a4aa1475122d16b6ac8a84f771e004e1a53e1df982d3dd069b262c9815ebb4')

prepare() {
  mv "${_realname}-${_realname}-${pkgver}" "${_realname}-${pkgver}"
  cd "${srcdir}"/$_realname-$pkgver

  # https://github.com/msys2/MINGW-packages/issues/8473
  patch -R -p1 -i "${srcdir}"/0004-default-pure-static.patch

  patch -p1 -i "${srcdir}"/0002-size-t-format.patch
  patch -p1 -i "${srcdir}"/0003-printf-format.patch
}

build() {
  mkdir "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("--buildtype=release")
  else
    _extra_config+=("--buildtype=debug")
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson setup \
    --prefix="${MINGW_PREFIX}" \
    "${_extra_config[@]}" \
    -Dtests=disabled \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/meson compile
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson install

  cp "${pkgdir}${MINGW_PREFIX}"/bin/pkgconf "${pkgdir}${MINGW_PREFIX}"/bin/pkg-config
  cp "${pkgdir}${MINGW_PREFIX}"/bin/pkgconf "${pkgdir}${MINGW_PREFIX}"/bin/${MINGW_CHOST}-pkgconf
  cp "${pkgdir}${MINGW_PREFIX}"/bin/pkg-config "${pkgdir}${MINGW_PREFIX}"/bin/${MINGW_CHOST}-pkg-config
}
